{extend name="main"}
{block name="body"}

<div class="bg-white mb-3">

    <el-tabs v-model="type" @tab-click="handleClick" class="my-3" style="margin-left: 10px;">
        <el-tab-pane label="客户欢迎语" name="1"></el-tab-pane>
        <el-tab-pane label="客户群欢迎语" name="2"></el-tab-pane>
    </el-tabs>
    <el-form :model="search" @submit.native.prevent="submitForm" ref="search" label-position="right" class="flex py-3">
        <el-form-item label="群名称" prop="title" class="flex mb-0 pl-3">
            <el-input v-model="search.title" placeholder="请输入你想搜索的关键词"></el-input>
        </el-form-item>
        <el-form-item class="flex mb-0 pl-3">
            <el-button type="primary" @click="initHtml">查询</el-button>
            <el-button @click="resetForm('search')">重置</el-button>
        </el-form-item>
    </el-form>
    <div style="background-color:#fff7f0;color: #f59A23;margin-bottom: 5px;">
        <div style="flex: 1 1 0%;padding: 20px">
            <span>因企业微信接口限制，在【企业微信后台】群欢迎语素材库进行编辑或删除操作，系统不会同步更新，建议在群欢迎语素材库进行管理。</span>
        </div>
    </div>
</div>
<div class="bg-white p-3">
    <div class="table-responsive pb-2">
        <el-button type="primary" size="mini" @click="window.open('/work-wechat/addWelcomeGroupMsg')">新建入群欢迎语</el-button>
    </div>
    <div class="table-responsive">
        <el-table ref="tableList" :data="tableData" v-loading="search.ajax" style="width: 100%;"
            header-cell-class-name="table-header-DDE6FB">

            <el-table-column prop="title" label="欢迎语名称">
            </el-table-column>
            <el-table-column prop="msg_type" label="欢迎语类型" width="200">
            </el-table-column>
            <el-table-column label="创建人">
                <template slot-scope="scope">
                    <div class="small">
                        <el-tag v-for="(item,i) in scope.row.user" class="m-1">{{item.name}}</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="修改时间">
                <template slot-scope="scope">
                    <div class="small">
                        <div>{{ scope.row.update_time }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="创建时间">
                <template slot-scope="scope">
                    <div class="small">
                        <div>{{ scope.row.create_time }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <div v-if="!scope.row.delete_time">
                        <el-button type="primary" plain size="mini"
                            @click="window.open('/work-wechat/modifyWelcomeGroupMsg/'+scope.row.id)">
                            编辑
                        </el-button>
                        <el-button type="danger" plain size="mini" @click="delWelcome(scope.row.id)">
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <div class="table-responsive py-3 text-center">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="search.page" :page-sizes="[10,50,100,200]" :page-size="search.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="search.total">
        </el-pagination>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    var vm = new Vue({
        el: '#app',
        data: {
            view: XYBase.view,

            labelPosition: 'right',

            defaultProps: {
                value: 'id',
                label: 'title',
                children: 'data'
            },
            type: '2',
            search: {
                title: '',
                // status:'all',
                type: '2',
                ajax: false,
                page: 1,
                total: 0,
                pageSize: 10
            },
            tableData: [],
        },
        watch: {
            view: XYBase.viewWatch,
            type(val, oldVal) {
                if (val == 1) {
                    window.location.href = '/work-wechat/welcomeList';
                }
            }
        },
        created() {
            XYBase.created(this);
        },
        methods: {

            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            resetForm: function (formName) {
                this.$refs[formName].resetFields();
            },
            handleSizeChange(val) {
                this.search.page = 1;
                this.search.pageSize = val;
                this.initHtml();
            },
            handleCurrentChange(val) {
                this.search.page = val;
                this.initHtml();
            },
            //切换选项卡
            handleClick(tab, event) {
                // console.log(tab, event);
                if (tab == 1) {
                    window.location.href = '/work-wechat/welcomeList';
                }
            },
            //删除欢迎问候语
            delWelcome(id) {
                console.log(id);
                var _this = this;
                this.$confirm('删除后对应的使用人员在加好友将无法自动发送欢迎消息，确认删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.ajax({
                        url: '/work-wechat/delWelcomeMsg',
                        type: 'post',
                        data: { id: id },
                        dataType: 'json',
                        error: function (err) {
                            XYBase.error(XYBase.config.system_network_error);
                        },
                        success: function (ret) {
                            if (ret) {
                                if (ret.code === 0) {
                                    XYBase.success("删除数据成功")
                                    _this.initHtml();
                                } else {
                                    XYBase.msg(ret.msg);
                                }
                            } else {
                                XYBase.error(XYBase.config.system_network_error);
                            }
                        }
                    });

                }).catch(() => { });//注意这里，这里是重点！！！
            },
            initHtml() {
                var _this = this;
                _this.search.ajax = true;
                $.ajax({
                    url: '',
                    type: 'get',
                    data: _this.search,
                    dataType: 'json',
                    error: function (err) {
                        _this.search.ajax = false;
                        XYBase.error(XYBase.config.system_network_error);
                    },
                    success: function (ret) {
                        _this.search.ajax = false;
                        if (ret) {
                            if (ret.code === 0) {
                                _this.search.total = ret.data.total;
                                _this.tableData = ret.data.data;
                            } else {
                                XYBase.msg(ret.msg);
                            }
                        } else {
                            XYBase.error(XYBase.config.system_network_error);
                        }
                    }
                });
            }
        }
    });
    $(function () {
        vm.initHtml();
        reloadWindowSize();
        $(window).resize(function () {
            reloadWindowSize();
        });
    });
    function reloadWindowSize(argument) {
        var winW = $(window).width();
        if (winW <= 576) {
            vm.labelPosition = 'top';
        } else {
            vm.labelPosition = 'right';
        }
    }
</script>
{/block}